/*
 * Copyright (c) 2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 */
package org.scharlessantos.atlas;

import org.scharlessantos.hermes.Hermes;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * Atlas is a free translation api for Java Sources licensed by new BSD License. Atlas is free to
 * use,
 * modify and resdistribute, but preserve the original author<br>
 * To use Atlas as a Java Project, must uses Hermes to. Hermes can downloaded at <a
 * href='https://code.google.com/p/hermes-logger/'>https://code.google.com/p/hermes-logger</a> <br>
 * <br>
 * * <br>
 * Atlas is licensed by new BSD License. A notice of license can be saw below<br>
 * <br>
 * <i>
 * Copyright (c) 2012-2013, Charles Santos da Silva <silva.charlessantos@gmail.com> -
 * scharlessantos.org
 * All rights reserved.<br>
 * <br>
 * Redistribution and use in source and binary forms, with or without<br>
 * modification, are permitted provided that the following conditions are met:<br>
 * <br>
 * 1. Redistributions of source code must retain the above copyright notice,<br>
 * this list of conditions and the following disclaimer.<br>
 * 2. Redistributions in binary form must reproduce the above copyright<br>
 * notice, this list of conditions and the following disclaimer in the<br>
 * documentation and/or other materials provided with the distribution.<br>
 * <br>
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"<br>
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE<br>
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE<br>
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE<br>
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR<br>
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF<br>
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS<br>
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN<br>
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)<br>
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE<br>
 * </i>
 * 
 * @author Charles Santos da Silva
 */
public class Atlas {
	
	private final Map<Language, Translation> translations = new HashMap<Language, Translation>();
	private final Language                   lang;
	
	/**
	 * Define a default language with Atlas will work
	 * 
	 * @param lang Deafult language
	 */
	public Atlas(Language lang) {
		this.lang = lang;
	}
	
	/**
	 * Load translations for specified language from file
	 * 
	 * @param lang Language from translations
	 * @param input File who load translations
	 */
	public void loadTranslations(Language lang, InputStream input) {
		if (translations.get(lang) == null) {
			Translation translation = new Translation(lang);
			if (translation.loadTranslations(input))
				translations.put(lang, translation);
		} else {
			Hermes.warn("Atlas already contains translations for language " + lang.toString());
		}
	}
	
	/**
	 * Translate key for Language<br>
	 * If translation for key not exists in language specified, returns key
	 * 
	 * @param lan Language to translate
	 * @param key Key to translate
	 * @return translation for Language/key
	 */
	public String translate(Language lan, String key) {
		return translate(lan, key, new Object[0]);
	}
	
	public String translate(Language lan, String key, Object... args) {
		if (lan == null)
			lan = lang;
		
		if (lan == null)
			Hermes.error("Must specified a language to translate");
		else if (translations.get(lan) != null)
			return translations.get(lan).translate(key, args);
		else
			Hermes.warn("Missing translation (%s): %s", lan.toString(), key);
		
		return key;
	}
	
	/**
	 * Remove all translations for language specified
	 * 
	 * @param lan Language to remove translations
	 */
	public void removeTranslations(Language lan) {
		if (translations.containsKey(lan))
			translations.remove(lan);
	}
}
